package com.chaodev.base.controller;

import com.chaodev.base.base.RedisDao;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.ResultEnum;
import com.chaodev.base.model.SysServer;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/sys")
public class SysController {
    @Autowired
    private RedisDao redisDao;
    @ApiOperation(value = "获取服务器信息",notes="返回SysServer实体集合数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    })
    @RequestMapping("/getServer")
    public Result getServer(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader("token");
        Result result = new Result();
        if (redisDao.existsKey(token)) {
            try {
                SysServer server = new SysServer();
                server.copyTo();
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
                result.setData(server);
            }catch (Exception e){
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("操作失败，失败原因："+e.getMessage());
                result.setData("[]");
            }
        }
        else{
            result.setCode(ResultEnum.AUTHORIZE_FAILURE.getCode());
            result.setMsg("身份认证失败");
        }
        return result;
    }
}
